﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sennit.SharepointOnlineHelper;
using Sennit.SharepointAutoMapper;
using Sennit.SharepointOnlineHelper.Connection;
using Sennit.SharepointOnlineHelper.Helper;
using ExampleMvcWithSharepointOnline.Models;
using Microsoft.SharePoint.Client;
using System.Linq.Expressions;
using System.Xml.Linq;

namespace ExampleMvcWithSharepointOnline.Controllers
{
    public class HomeController : Controller
    {
        [SharepointListName("Teste")]
        public class Uf : IEntitySharepointMapper
        {
            [SharepointFieldName("ID")]
            public Int32? Id { get; set; }

            [SharepointFieldName("Title")]
            public String Name { get; set; }



        }

       

      

        public ActionResult Index()
        {

            SharepointAuth AuthParameters = new SharepointAuth()
            {
                UrlApp = "https://Sennitltda-099e2dd1524896.sharepoint.com",
                Senha = "",
                Usuario = ""

            };

            Uf uf = new Uf() { Name = "TP2" };
            string caml = uf.BuildCamlQuery<Uf>(p => p.Name == "1");
             MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper(AuthParameters.UrlApp, AuthParameters.Usuario, AuthParameters.Senha);
            using (ClientContext clientContext = new ClientContext("https://Sennitltda-099e2dd1524896.sharepoint.com"))
            {
                clientContext.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
                Web web = clientContext.Web;


                List listNotas = web.Lists.GetByTitle("Uf");
                ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
                Microsoft.SharePoint.Client.ListItem itemNota = listNotas.AddItem(itemCreationInfo);
                string nome = uf.GetSharepointListName();
                itemNota.ProjectListItemFromEntity<Uf>(uf);

                itemNota.Update();
                clientContext.Load<List>(listNotas);
                clientContext.Load<Microsoft.SharePoint.Client.ListItem>(itemNota);
                clientContext.ExecuteQuery();

            }

            //SharepointList spList = new SharepointList(AuthParameters);

            //List<Uf> states = spList.Get("Uf", "", null).ProjectToListEntity<Uf>();



            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
